<template>
	<el-dialog :title="dialogTitle" :visible.sync="visible" top="0" width="560px" append-to-body>
		<el-form ref="form" size="medium" :model="formData" :rules="formRules" label-width="80px">
			<el-row :gutter="16">
				<el-col :span="24">
					<el-form-item label="配置名称" prop="label">
						<el-input v-model="formData.label" placeholder="请输入配置名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="配置字段" prop="code">
						<el-input v-model="formData.code" placeholder="请输入配置字段"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="适用类型" prop="scopeType">
						<el-select v-model="formData.scopeType" placeholder="请选择适用类型">
							<el-option v-for="(item, index) in applyTypeList" :key="index" :label="item.label" :value="item.type" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="配置类型" prop="type">
						<el-select v-model="formData.type" placeholder="请选择配置类型">
							<el-option v-for="(item, index) in configTypeList" :key="index" :label="item.label" :value="item.type" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="排序">
						<el-input-number v-model="formData.orderBy" controls-position="right" :min="1" style="width: 150px" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" :loading="loading" @click="handleSubmitForm()">{{ formTypeName }}</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { applyType, configType } from '@/utils/constants';
import { addedBaseInfo, updateBaseInfo } from '@/api/system/sysBaseInfo';
export default {
	props: {
		value: { type: Boolean, default: false },
		data: { type: Object, default: () => {} }
	},
	data() {
		return {
			visible: this.value,
			loading: false,
			formData: {},
			formRules: {
				label: [{ required: true, message: '请输入网站配置名称', trigger: 'blur' }],
				code: [{ required: true, message: '请选择网站配置字段', trigger: 'blur' }],
				scopeType: [{ required: true, message: '请选择适用类型', trigger: 'change' }],
				type: [{ required: true, message: '请选择网站配置类型', trigger: 'change' }]
			}
		};
	},
	computed: {
		addOrEdit() {
			return Object.keys(this.data).length === 0;
		},
		callFunction() {
			return eval(this.addOrEdit ? addedBaseInfo : updateBaseInfo);
		},
		applyTypeList() {
			return applyType;
		},
		configTypeList() {
			return configType;
		},
		formTypeName() {
			return this.addOrEdit ? '新增' : '修改';
		},
		dialogTitle() {
			return this.addOrEdit ? '新增网站配置' : '修改网站配置';
		}
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		visible(flag) {
			this.$emit('input', flag);
			if (!flag) return false;
			this.$nextTick(() => {
				this.$refs['form'].resetFields();
				this.formData = { ...this.data };
			});
		}
	},
	methods: {
		handleSubmitForm() {
			this.$refs['form'].validate((valid) => {
				if (!valid) return false;
				this.loading = true;
				this.submitFormData();
			});
		},

		async submitFormData() {
			let params = { ...this.formData };
			let result = await this.callFunction(params).then((res) => res.data);
			this.loading = false;
			if (result.code === '0') {
				this.$message.success(`${this.formTypeName}成功`);
				this.$store.dispatch('getSiteBaseInfo');
				this.visible = false;
				this.$emit('refresh');
			}
		}
	}
};
</script>
